package com.usooft.metaTalent.core.base.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Component
@Slf4j
public class DiskFileProvider implements FileProvider {

    private final static String ROOT = ".files";

    @Override
    public String getPrefix() {
        return "disk";
    }

    @Override
    public File load(String path) {
        return new File(ROOT, path);
    }

    @Override
    public void store(String path, InputStream is) throws IOException {
        File outFile = new File(ROOT, path);
        FileUtils.forceMkdirParent(outFile);
        FileUtils.copyToFile(is, outFile);
    }

}
